package com.qianbao.controller.v1.order;


import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.domain.order.OrderEntity;
import com.qianbao.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Controller
@ApiVersion(1)
@RequestMapping("/ordercenter/{version}/")
public class OrderController {


    @Autowired
    private OrderService service;


    @RequestMapping("/save-order")
    @ResponseBody
    public Result saveOrder(@Valid OrderEntity entity){
        try {
            service.saveOrder(entity);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }


    @RequestMapping("/find-order")
    @ResponseBody
    public Result findOrderList(String customId,String state){

        try {
            List<OrderEntity> orders = service.findOrderWithOrderStateCustomId(state, customId);
            HashMap<String,List> map = new HashMap<>();
            map.put("list",orders);
            return Result.success(map);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }

    @RequestMapping("/update-order-state")
    @ResponseBody
    public  Result updateOrderState(String state,String orderId){
        try {
            service.updateOrderState(orderId,state);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }



}
